package com.example.demo.practice;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liweizhi
 * @date 2020/4/28
 */
public class Temp {

    private static final Pattern PATTERN_IP = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
    private static final Map<String, String> RANDOM_IP_MAP = new HashMap<>(16);

    public static void main(String[] args) {
//        String str = "中文";
//        System.out.println(new String(str));

        Map<String, Object> map = new HashMap<>();
        map.put("ip", "10.12.34.156");
        map.put("ip2", "10.12.34.255");
        map.put("name", "中文aaa");
        randomIp(map, "ip");
        randomIp(map, "ip2");
        randomLetter(map, "name");
        System.out.println(map);
    }

    public static void randomIp(Map<String, Object> map, String key) {
        Object o = map.get(key);
        if (o == null) {
            return;
        }
        String value = (String) o;
        value = value.trim();
        if (value.length() == 0) {
            return;
        }

        Matcher matcher = PATTERN_IP.matcher(value);
        if (matcher.find()) {
            String ipFrom = matcher.group();
            String ipTo = RANDOM_IP_MAP.get(ipFrom);
            if (ipTo == null) {
                String[] split = ipFrom.split("\\.");
                StringBuilder sb = new StringBuilder(15);
                for (int i = 0; i < split.length; i++) {
                    sb.append((Integer.parseInt(split[i]) + 1) % 255);
                    if (i < split.length - 1) {
                        sb.append(".");
                    }
                }
                ipTo = sb.toString();
                RANDOM_IP_MAP.put(ipFrom, ipTo);
            }
            map.put(key, value.replace(ipFrom, ipTo));
        }
    }

    public static void randomLetter(Map<String, Object> map, String key) {
        Object o = map.get(key);
        if (o == null) {
            return;
        }
        String value = (String) o;
        value = value.trim();
        if (value.length() == 0) {
            return;
        }
        char[] charArray = value.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            // A-Z 65-90
            // a-z 97-122
            if ((c >= 65 && c < 90) || (c >= 97 && c < 122)) {
                charArray[i] = (char) (c + 1);
            } else if (c == 90) {
                charArray[i] = (65);
            } else if (c == 122) {
                charArray[i] = (97);
            } else {
                charArray[i] = c;
            }
        }
        map.put(key, new String(charArray));
    }

}
